package com.ruoyi.web.controller.me;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ProcessStopException;
import com.ruoyi.dj.mapper.TerrMagnetismMapper;
import com.ruoyi.me.service.SpatialIndexForecastService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * </p>
 *
 * @author liuhl
 * @since 2024-06-16
 */

@RestController
@RequestMapping("/me/spatialIndex")
@Api(value = "SpatialIndexForecastController", tags = "模型检验评估-空间环境指数预报评估")
public class SpatialIndexForecastController {

    @Resource
    private SpatialIndexForecastService spatialIndexForecastService;
    @Resource
    private TerrMagnetismMapper queryMapper;


    @GetMapping("/timeLimit")
    @ApiOperation(value = "获取时间范围上界限", notes = "获取时间范围上界限", httpMethod = "GET")
    public AjaxResult timeLimit(String indexType) {

        String tableName = "SWDATA_OBS_" + indexType;
        String predictTime = queryMapper.getPredictTime(tableName, null).substring(0,10);
        //查出表中最大时间，如果不是 yyyy-MM-dd 则 截取 yyyy-MM-dd 返回前端。
        return AjaxResult.success(predictTime);
    }

    //@PreAuthorize("@ss.hasPermi('me:spatialIndex:getCalculateResult')")
    @ApiOperation(value = "获取计算评估结果", notes = "获取计算评估结果")
    @PostMapping("/getCalculateResult")
    public AjaxResult getCalculateResult(@RequestBody Map<String, Object> map) throws ProcessStopException {
        return spatialIndexForecastService.getCalculateResult(map);
    }
    //@PreAuthorize("@ss.hasPermi('me:spatialIndex:download')")
    @ApiOperation(value = "下载报告", notes = "下载报告")
    @PostMapping("/download")
    public AjaxResult download(@RequestBody Map<String, Object> map){
        return spatialIndexForecastService.download(map);
    }
    //@PreAuthorize("@ss.hasPermi('me:spatialIndex:subscribe')")
    @ApiOperation(value = "建立连接", notes = "建立连接")
    @GetMapping(path = "subscribe", produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
    public SseEmitter push(String id, String flag) {
        return spatialIndexForecastService.push(id,flag);
    }
}
